var express = require('express')
var subs = require('../models/sub')
var moment = require('moment')

var router = express.Router()

router.get('/', async function(req,res,next){
	res.setHeader("Access-Control-Allow-Origin", "*");
	res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
	try{
		let result1 = await subs.aggregate([
		{
			$match:{tem:{ $gte : 100 } } 
		},
		{
			$project:{_id:1,adress:1,name:1,sub_num:1,long:1,lat:1,tem:1,tilt:1,created_time:1,update_time:1,message:'可能发生火灾！'}
		}])
	let result2 = await subs.aggregate([
		{
			$match:{tilt:{ $gte : 5,$lt:10 } } 
		},
		{
			$project:{_id:1,adress:1,name:1,sub_num:1,long:1,lat:1,tem:1,tilt:1,created_time:1,update_time:1,message:'可能发生山体滑坡！'}
		}])
	let result3  = await subs.aggregate([
		{
			$match:{tilt:{ $gte : 10 }} 
		},
		{
			$project:{_id:1,adress:1,name:1,sub_num:1,long:1,lat:1,tem:1,tilt:1,created_time:1,update_time:1,message:'可能发生地震！'}
		}])
	let result4 = await subs.aggregate([
		{
			$match:{tilt:{ $lt : 5 },tem:{$lt:100} } 
		},
		{
			$project:{_id:1,adress:1,name:1,sub_num:1,long:1,lat:1,tem:1,tilt:1,created_time:1,update_time:1,message:'目前没有异常！'}
		}])

	return res.status(200).json({result1,result2,result3,result4})

}catch(error){
	next(error)
}
})

router.get('/selectList_24hour',function(req,res,next){
	let results = new Array(6).fill(0)

	var startdate= moment().subtract(1, "days").toISOString()
	var enddate = moment().toISOString()

	subs.find({
				$or:[
				{tem:{$gte:100}},{tilt:{$gte:5}}
				]
			},function(error,result){
		if(error){
			 next(error)
		}
		for(let key in result){
			if(moment(result[key].update_time).isBetween(startdate,enddate))
				results[key] = result[key]
				}
	res.setHeader("Access-Control-Allow-Origin", "*");
	res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
				return res.status(200).json(results)	
	})
	
})

router.get('/selectList_48hour',function(req,res,next){
	var results = new Array(10).fill(0)

	var startdate= moment().subtract(2, "days").toISOString()
	var enddate = moment().toISOString()

	subs.find({
				$or:[
				{tem:{$gte:100}},{tilt:{$gte:5}}
				]
			},function(error,result){
		if(error){
			 next(error)
		}
		for(let key in result){
			if(moment(result[key].update_time).isBetween(startdate,enddate))
				results[key] = result[key]
				}	
	res.setHeader("Access-Control-Allow-Origin", "*");
	res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
	//console.log('extendAPIOutput')

		return res.status(200).json(results)	
	})
})

router.get('/selectList_7day',function(req,res,next){
	var results = new Array(10).fill(0)

	var startdate= moment().subtract(7, "days").toISOString()
	var enddate = moment().toISOString()

	subs.find({
				$or:[
				{tem:{$gte:100}},{tilt:{$gte:5}}
				]
			},function(error,result){
		if(error){
			 next(error)
		}
		for(let key in result){
			if(moment(result[key].update_time).isBetween(startdate,enddate))
				results[key] = result[key]
				}
	res.setHeader("Access-Control-Allow-Origin", "*");
	res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
	//console.log('extendAPIOutput')
	
		return res.status(200).json(results)	
	})
})

router.get('/selectList_30day',function(req,res,next){
	var results = new Array(10).fill(0)

	var startdate= moment().subtract(30, "days").toISOString()
	var enddate = moment().toISOString()

	subs.find({
				$or:[
				{tem:{$gte:100}},{tilt:{$gte:5}}
				]
			},function(error,result){
		if(error){
			 next(error)
		}
		for(let key in result){
			if(moment(result[key].update_time).isBetween(startdate,enddate))
				results[key] = result[key]
				}	
	res.setHeader("Access-Control-Allow-Origin", "*");
	res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
	//console.log('extendAPIOutput')

		return res.status(200).json(results)	
	})
})

router.get('/selectList_1year',function(req,res,next){
	var results = new Array(10).fill(0)

	var startdate= moment().subtract(1, "years").toISOString()
	var enddate = moment().toISOString()

	subs.find({
				$or:[
				{tem:{$gte:100}},{tilt:{$gte:5}}
				]
			},function(error,result){
		if(error){
			 next(error)
		}
		for(let key in result){
			if(moment(result[key].update_time).isBetween(startdate,enddate))
				results[key] = result[key]
				}	
	res.setHeader("Access-Control-Allow-Origin", "*");
	res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
	//console.log('extendAPIOutput')

		return res.status(200).json(results)	
	})
})

router.get('/selectList_history_search',function(req,res,next){
	res.setHeader("Access-Control-Allow-Origin", "*");
	res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");

	var let1 = req.query.weidu1
	var let2 = req.query.weidu2
	var long1 = req.query.jingdu1
	var long2 = req.query.jingdu2
	var startdate = req.query.data1
	var enddata = req.query.data2

	var results = new Array(10).fill(0)

	subs.find({
				long:{$lt:long2,$gt:long1},let:{$lt:let2,$gt:let1}
			},function(error,result){
		if(error){
			 next(error)
		}
		for(let key in result){
			if(moment(result[key].update_time).isBetween(startdate,enddate))
				results[key] = result[key]
				}	
				return res.status(200).json(results)
			})
})

router.get('/selectList_history_fire',function(req,res,next){
	subs.find({
		tem:{$gte:100}
	},function(error,result){
		if(error){
			next(error)
		}
	res.setHeader("Access-Control-Allow-Origin", "*");
	res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
	//console.log('extendAPIOutput')

		return res.status(200).json({
			result:result,
			message:'可能发生火灾！'
		})
	})
})

router.get('/selectList_history_earthquake',function(req,res,next){
	subs.find({
		tilt:{$gte:10}
	},function(error,result){
		if(error){
			next(error)
		}	
	res.setHeader("Access-Control-Allow-Origin", "*");
	res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
	//console.log('extendAPIOutput')

		return res.status(200).json({
			result:result,
			message:'可能发生地震！'
		})
	})
})

router.get('/selectList_history_landslide',function(req,res,next){
	subs.find({
		tilt: {$lt :10, $gt : 5}
	},function(error,result){
		if(error){
			next(error)
		}		
	res.setHeader("Access-Control-Allow-Origin", "*");
	res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
	//console.log('extendAPIOutput')

		return res.status(200).json({
			result:result,
			message:'可能发生山体滑坡！'
		})
	})
})
module.exports = router
